from apps.leadnews.models import Article
from apps.leadnews.serializers import CollectArticleSerializer
from .models import Label, Question, Reply
from rest_framework import serializers
from apps.user.models import User


class QuestionModelSerializer(serializers.ModelSerializer):
    labels = serializers.StringRelatedField(many=True, read_only=True)
    user = serializers.StringRelatedField(read_only=True)

    class Meta:
        model = Question
        fields = "__all__"


class QuestionCreateModelSerializer(serializers.ModelSerializer):
    user = serializers.StringRelatedField(read_only=True)

    class Meta:
        model = Question
        fields = "__all__"

    def create(self, validated_data):
        request = self.context.get('request')
        user_id = request.user["user_id"]
        labels = validated_data.pop("labels")
        instance = Question.objects.create(user_id=user_id, **validated_data)
        for label in labels:
            instance.labels.add(label)
        instance.save()
        return instance


class QuestionUserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ["id", "username", "avatar"]


class SubCommentsSerializer(serializers.ModelSerializer):
    user = QuestionUserSerializer()

    class Meta:
        model = Reply
        fields = "__all__"


class QuestionCommentSerializer(serializers.ModelSerializer):
    subs = serializers.StringRelatedField(many=True)
    user = QuestionUserSerializer()

    class Meta:
        model = Reply
        fields = "__all__"


class QuestionAnswerSerializer(serializers.ModelSerializer):
    subs = serializers.SerializerMethodField()
    user = QuestionUserSerializer()

    class Meta:
        model = Reply
        fields = "__all__"

    def get_subs(self, obj):
        subs = obj.subs.filter(parent__isnull=False, type=1)
        return SubCommentsSerializer(subs, many=True, read_only=True).data


class QuestionDetialSerializer(serializers.ModelSerializer):
    labels = serializers.StringRelatedField(many=True)
    comment_question = serializers.SerializerMethodField()
    answer_question = serializers.SerializerMethodField()
    user = serializers.StringRelatedField()

    class Meta:
        model = Question
        fields = ["id", "createtime", "labels", "reply", "replyname", "replytime", "title",
                  "unuseful_count", "useful_count", "user", "visits", "content",
                  "comment_question", "answer_question"]

    def get_comment_question(self, obj):
        replies = obj.replies.filter(parent__isnull=True, type=0)
        return QuestionCommentSerializer(replies, many=True, read_only=True).data

    def get_answer_question(self, obj):
        replies = obj.replies.filter(parent__isnull=True, type=2)
        return QuestionAnswerSerializer(replies, many=True, read_only=True).data


class ReplyModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Reply
        fields = "__all__"

    def create(self, validated_data):
        request = self.context.get("request")
        user_id = request.user.id
        reply = Reply.objects.create(user_id=user_id, **validated_data)
        question = Question.objects.get(id=reply.problem_id)
        question.reply += 1
        question.solve = 1
        question.save()
        return reply


class LabelsModelSerializer(serializers.ModelSerializer):
    users = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
    questions = serializers.SerializerMethodField()
    articles = CollectArticleSerializer(many=True,read_only=True)

    class Meta:
        model = Label
        fields = "__all__"

    def get_questions(self, obj):
        questions = obj.questions.all()
        return QuestionModelSerializer(questions, many=True, read_only=True).data

